package com.example.activity.controller;

import com.example.activity.dao.AppFormDao;
import com.example.activity.pojo.ApplForm;
import com.example.activity.service.UserService;
import com.example.activity.service.UserTaskService;
import com.example.activity.service.WorkFlowService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.example.activity.pojo.GeaUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author Create by Ron
 * @date 2020/5/13 10:12
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AppFormDao appFormDao;


    @Autowired
    private UserTaskService userTaskService;


    @Autowired
    private TaskService taskService;


    @Autowired
    private WorkFlowService workFlowService;

    /**
     * 去提交表单页
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("toApplForm")
    public ModelAndView toApplForm(ModelAndView modelAndView) {

        modelAndView.setViewName("appl-form");

        return modelAndView;
    }


    /**
     * 用户选择登录页
     *
     * @return
     */
    @RequestMapping("loginPage")
    public ModelAndView login(ModelAndView modelAndView) {
        modelAndView.setViewName("login-page");
        modelAndView.addObject("geaUserList", userService.getGeaUserList());
        return modelAndView;
    }


    /**
     * 登入 进入用户主页
     *
     * @param userid
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping("login/{id}")
    public ModelAndView login(@PathVariable("id") int userid, ModelAndView modelAndView, HttpServletRequest request) {

        GeaUser geaUser = userService.getUserById(userid);

        request.getSession().setAttribute("geaUser", geaUser);

        List<Task> tasks = workFlowService.queryMyTasks(geaUser.getName());

      //  Set<String> formIdsByGeaUserId = appFormDao.findFormIdsByGeaUserId(userid);
      //  List<HistoricProcessInstance> historicProcessInstances = workFlowService.queryMyHiProcess(formIdsByGeaUserId);
       // modelAndView.addObject("hiPros",historicProcessInstances);
        modelAndView.addObject("tasks", tasks);
        modelAndView.setViewName("user-home");

        return modelAndView;
    }


    /**
     * 保存表单数据
     *
     * @param applForm
     * @return
     */
    @RequestMapping(value = "saveForm", method = RequestMethod.POST)
    public void saveForm(@ModelAttribute("applForm") ApplForm applForm, HttpServletResponse response) throws IOException {
        Map map = new HashMap();
        log.info("=================================: " + applForm.getBeginDate());
        ApplForm applForm1 = appFormDao.save(applForm);
        applForm1.setAppStatus("approve");
        map.put("applForm", applForm1);
        map.put("nextApprover", applForm1.getGeaUserId());
        workFlowService.startSimpleProcess("leaveAppl", applForm1.getFormId() + "", map);
        response.sendRedirect("/");
    }


    /**
     * 完成 审批任务
     *
     * @param taskId
     * @param status
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/completeTask")
    public void completeTask(@RequestParam("taskId") String taskId, @RequestParam("status") String status, HttpServletRequest request, HttpServletResponse response) throws IOException {

        Map map = workFlowService.getTaskVariables(taskId);
        ApplForm applForm = (ApplForm) map.get("applForm");

        applForm.setAppStatus(status);
        map.put("applForm", applForm);
        System.out.println(map);
        workFlowService.completeTask(taskId, map);

        GeaUser geaUser = (GeaUser) request.getSession().getAttribute("geaUser");

        response.sendRedirect("/login/" + geaUser.getId());
    }


}
